<?php

namespace App\Http\Controllers\Admin\Ad;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Ad\Advert;
use App\Models\Ad\Category;
use App\Http\Requests\Ad\StoreAdvert;
use App\Http\Requests\Ad\UpdateAdvert;


class AdvertController extends Controller
{
    function __construct()
    {
        view()->share([
            '_ads'=>'am-in',
            '_advert'=>'am-active',
            'categories' => Category::orderBy('id','desc')->get()
        ]);
    }


    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $adverts = Advert::with('category')->orderBy('sort_order')->paginate(5);
        return view('admin.ad.advert.index', compact('adverts'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
//        $categories = Category::orderBy('sort_order')->get();
        return view('admin.ad.advert.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @return \Illuminate\Http\Response
     */
    public function store(StoreAdvert $request)
    {
        Advert::create($request->all());
        return redirect(route('ad.advert.index'))->with('success', '新增广告成功');
    }

    /**
     * Display the specified resource.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
//        $categories = Category::orderBy('sort_order')->get();
        $advert = Advert::find($id);
        return view('admin.ad.advert.edit', compact('advert'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function update(UpdateAdvert $request, $id)
    {
        $advert = Advert::find($id);
        $advert->update($request->all());
        return redirect(route('ad.advert.index'))->with('success', '编辑成功');

    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        Advert::destroy($id);
    }

    /***
     * 多选软删除
     */
    public function delete_all(Request $request)
    {
        $id = $request->checked_id;
        Advert::destroy($id);
    }

    /***
     * 排序
     */
    public function sort_order(Request $request)
    {
        $advert = Advert::find($request->id);
        $advert->sort_order = $request->sort_order;
        $advert->save();
    }

    /***
     * 加载回收站首页
     */
    public function trash()
    {
        view()->share([
           '_ads'=>'am-in',
           '_advert'=>'',
           '_trash'=>'am-active'
        ]);

        $adverts = Advert::onlyTrashed()->with('category')->orderBy('sort_order')->paginate(3);
        return view('admin.ad.advert.trash', compact('adverts'));
    }

    /***
     * 单选彻底删除
     */
    public function delete_one($id)
    {
        Advert::withTrashed()->where('id', $id)->forceDelete();
        return back();
    }

    /***
     * 多选彻底删除
     */
    public function delete(Request $request)
    {
        Advert::whereIn('id', $request->checked_id)->forceDelete();
    }

    /***
     * 单选还原
     */
    public function restore_one(Request $request)
    {
        Advert::where('id', $request->id)->restore();
    }

    /***
     * 多选还原
     */
    public function restore_all(Request $request)
    {
        Advert::whereIn('id', $request->checked_id)->restore();
    }
}
